package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminBrandService;
import com.cskaoyan.service.AdminBrandServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@WebServlet("/admin/brand/*")
public class AdminBrandServlet extends WdBaseServlet {
    private AdminBrandService adminBrandService = new AdminBrandServiceImpl();

    @SneakyThrows
    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        MarketBrand marketBrand = new MarketBrand();
        Map<String, String[]> parameterMap = request.getParameterMap();
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        if (id != null&&!"".equals(id)) {
            marketBrand.setId(Integer.parseInt(id));
        }
        if (name != null &&! "".equals(name)){
            marketBrand.setName(name);
        }
        CommonData data = adminBrandService.query(Integer.parseInt(page), Integer.parseInt(limit), marketBrand, sort, order);

        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response){
        String s = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(s, MarketBrand.class);
        if(marketBrand.getDesc()==null){
            return BaseRespVo.fail(401,"参数不对");
        }
        if(marketBrand.getPicUrl()==null){
            return BaseRespVo.fail(401,"参数不对");
        }
        if(marketBrand.getFloorPrice()==null){
            return BaseRespVo.fail(401,"参数不对");
        }
        MarketBrand data = adminBrandService.create(marketBrand);
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response){
        String s = request.getReader().readLine();
        MarketBrand read = JacksonUtil.read(s, MarketBrand.class);
        MarketBrand data =adminBrandService.update(read);
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response){
        String s = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(s, MarketBrand.class);
        adminBrandService.delete(marketBrand.getId());
        return BaseRespVo.ok();
    }

}
